<template>
  <div class="appointment-list">
    <el-card class="search-card">
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="服务名称">
          <el-input v-model="queryParams.serviceName" placeholder="请输入服务名称"></el-input>
        </el-form-item>
        <el-form-item label="预约状态">
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="待确认" value="PENDING"></el-option>
            <el-option label="已确认" value="CONFIRMED"></el-option>
            <el-option label="已完成" value="COMPLETED"></el-option>
            <el-option label="已取消" value="CANCELLED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="预约时间">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="handleDateChange">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-table
      v-loading="loading"
      :data="appointmentList"
      style="width: 100%; margin-top: 20px">
      <el-table-column prop="id" label="预约编号" width="100"></el-table-column>
      <el-table-column prop="serviceName" label="服务名称" width="150"></el-table-column>
      <el-table-column prop="petName" label="宠物名称" width="120"></el-table-column>
      <el-table-column prop="username" label="预约人" width="120"></el-table-column>
      <el-table-column prop="appointmentTime" label="预约时间" width="180"></el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleView(scope.row.id)">查看</el-button>
          <el-button
            v-if="scope.row.status === 'PENDING'"
            size="mini"
            type="success"
            @click="handleConfirm(scope.row.id)">确认</el-button>
          <el-button
            v-if="scope.row.status === 'CONFIRMED'"
            size="mini"
            type="primary"
            @click="handleComplete(scope.row.id)">完成</el-button>
          <el-button
            v-if="['PENDING', 'CONFIRMED'].includes(scope.row.status)"
            size="mini"
            type="danger"
            @click="handleCancel(scope.row.id)">取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px; text-align: right">
    </el-pagination>
  </div>
</template>

<script>
import { getAppointmentList, updateAppointmentStatus } from '@/api/service'

export default {
  name: 'AppointmentList',
  data() {
    return {
      loading: false,
      appointmentList: [],
      total: 0,
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceName: '',
        status: '',
        startTime: '',
        endTime: ''
      }
    }
  },
  created() {
    this.fetchAppointmentList()
  },
  methods: {
    async fetchAppointmentList() {
      this.loading = true
      try {
        const response = await getAppointmentList(this.queryParams)
        this.appointmentList = response.data.list
        this.total = response.data.total
      } catch (error) {
        console.error('获取预约列表失败:', error)
        this.$message.error('获取预约列表失败')
      } finally {
        this.loading = false
      }
    },
    handleDateChange(val) {
      if (val) {
        this.queryParams.startTime = val[0]
        this.queryParams.endTime = val[1]
      } else {
        this.queryParams.startTime = ''
        this.queryParams.endTime = ''
      }
    },
    handleSearch() {
      this.queryParams.pageNum = 1
      this.fetchAppointmentList()
    },
    handleReset() {
      this.dateRange = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        serviceName: '',
        status: '',
        startTime: '',
        endTime: ''
      }
      this.fetchAppointmentList()
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.fetchAppointmentList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.fetchAppointmentList()
    },
    handleView(id) {
      this.$router.push(`/appointment/${id}`)
    },
    async handleConfirm(id) {
      try {
        await this.$confirm('确认接受该预约吗？')
        await updateAppointmentStatus(id, 'CONFIRMED')
        this.$message.success('预约已确认')
        this.fetchAppointmentList()
      } catch (error) {
        if (error !== 'cancel') {
          console.error('确认预约失败:', error)
          this.$message.error('确认预约失败')
        }
      }
    },
    async handleComplete(id) {
      try {
        await this.$confirm('确认完成该预约吗？')
        await updateAppointmentStatus(id, 'COMPLETED')
        this.$message.success('预约已完成')
        this.fetchAppointmentList()
      } catch (error) {
        if (error !== 'cancel') {
          console.error('完成预约失败:', error)
          this.$message.error('完成预约失败')
        }
      }
    },
    async handleCancel(id) {
      try {
        await this.$confirm('确认取消该预约吗？', '提示', {
          type: 'warning'
        })
        await updateAppointmentStatus(id, 'CANCELLED')
        this.$message.success('预约已取消')
        this.fetchAppointmentList()
      } catch (error) {
        if (error !== 'cancel') {
          console.error('取消预约失败:', error)
          this.$message.error('取消预约失败')
        }
      }
    },
    getStatusType(status) {
      const statusMap = {
        PENDING: 'warning',
        CONFIRMED: 'primary',
        COMPLETED: 'success',
        CANCELLED: 'info'
      }
      return statusMap[status] || ''
    },
    getStatusText(status) {
      const statusMap = {
        PENDING: '待确认',
        CONFIRMED: '已确认',
        COMPLETED: '已完成',
        CANCELLED: '已取消'
      }
      return statusMap[status] || status
    }
  }
}
</script>

<style scoped>
.appointment-list {
  padding: 20px;
}
.search-card {
  margin-bottom: 20px;
}
</style> 